{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt #画图模板\n",
    "import matplotlib.dates as mdates #日期格式处理模块\n",
    "from matplotlib import style #用来自定义图表风格的模板\n",
    "\n",
    "#magic command\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'svg'#提高分辨率\n",
    "\n",
    "from matplotlib.font_manager import FontProperties # 画图自定义\n",
    "from pylab import mpl #画图自定义字体\n",
    "mpl.rcParams['font.family']='Times New Roman'# 指定默认字体"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td>NaN</td>\n",
       "      <td>96.0500</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>95.7900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>126000</td>\n",
       "      <td>4.940000e+05</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.9800</td>\n",
       "      <td>104.3000</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>19700</td>\n",
       "      <td>8.400000e+04</td>\n",
       "      <td>0.044109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.3900</td>\n",
       "      <td>109.0700</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>103.7300</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>2800</td>\n",
       "      <td>1.600000e+04</td>\n",
       "      <td>0.045407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.1300</td>\n",
       "      <td>113.5700</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>3200</td>\n",
       "      <td>3.100000e+04</td>\n",
       "      <td>0.049666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.5500</td>\n",
       "      <td>120.0900</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>1500</td>\n",
       "      <td>6.000000e+03</td>\n",
       "      <td>0.049760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8210 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1990-12-19        NaN    96.0500    99.9800    95.7900    99.9800   \n",
       "1990-12-20    99.9800   104.3000   104.3900    99.9800   104.3900   \n",
       "1990-12-21   104.3900   109.0700   109.1300   103.7300   109.1300   \n",
       "1990-12-24   109.1300   113.5700   114.5500   109.1300   114.5500   \n",
       "1990-12-25   114.5500   120.0900   120.2500   114.5500   120.2500   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  \n",
       "Day                                              \n",
       "1990-12-19       126000  4.940000e+05       NaN  \n",
       "1990-12-20        19700  8.400000e+04  0.044109  \n",
       "1990-12-21         2800  1.600000e+04  0.045407  \n",
       "1990-12-24         3200  3.100000e+04  0.049666  \n",
       "1990-12-25         1500  6.000000e+03  0.049760  \n",
       "...                 ...           ...       ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647  \n",
       "\n",
       "[8210 rows x 8 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('../000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format = '%Y/%m/%d')\n",
    "data.set_index('Day',inplace = True)\n",
    "data['Close'] = pd.to_numeric(data['Close'],errors = 'coerce')\n",
    "data['Preclose'] = (data['Close']).shift(1)\n",
    "data['Return']=(data['Close']-data['Preclose'])/data['Preclose']\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Monthly Return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Invalid frequency: ME",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4447\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'ME'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4549\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4453\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: ME",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m data_new \u001b[38;5;241m=\u001b[39m data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1995-01-01\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024-12-13\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[1;32m----> 2\u001b[0m Month_data \u001b[38;5;241m=\u001b[39m data_new\u001b[38;5;241m.\u001b[39mresample(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mME\u001b[39m\u001b[38;5;124m'\u001b[39m)[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x:(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39mx)\u001b[38;5;241m.\u001b[39mprod()\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m.\u001b[39mto_frame()\n\u001b[0;32m      3\u001b[0m Month_data\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:9439\u001b[0m, in \u001b[0;36mNDFrame.resample\u001b[1;34m(self, rule, axis, closed, label, convention, kind, on, level, origin, offset, group_keys)\u001b[0m\n\u001b[0;32m   9436\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   9437\u001b[0m     axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m-> 9439\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m get_resampler(\n\u001b[0;32m   9440\u001b[0m     cast(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSeries | DataFrame\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m),\n\u001b[0;32m   9441\u001b[0m     freq\u001b[38;5;241m=\u001b[39mrule,\n\u001b[0;32m   9442\u001b[0m     label\u001b[38;5;241m=\u001b[39mlabel,\n\u001b[0;32m   9443\u001b[0m     closed\u001b[38;5;241m=\u001b[39mclosed,\n\u001b[0;32m   9444\u001b[0m     axis\u001b[38;5;241m=\u001b[39maxis,\n\u001b[0;32m   9445\u001b[0m     kind\u001b[38;5;241m=\u001b[39mkind,\n\u001b[0;32m   9446\u001b[0m     convention\u001b[38;5;241m=\u001b[39mconvention,\n\u001b[0;32m   9447\u001b[0m     key\u001b[38;5;241m=\u001b[39mon,\n\u001b[0;32m   9448\u001b[0m     level\u001b[38;5;241m=\u001b[39mlevel,\n\u001b[0;32m   9449\u001b[0m     origin\u001b[38;5;241m=\u001b[39morigin,\n\u001b[0;32m   9450\u001b[0m     offset\u001b[38;5;241m=\u001b[39moffset,\n\u001b[0;32m   9451\u001b[0m     group_keys\u001b[38;5;241m=\u001b[39mgroup_keys,\n\u001b[0;32m   9452\u001b[0m )\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:1969\u001b[0m, in \u001b[0;36mget_resampler\u001b[1;34m(obj, kind, **kwds)\u001b[0m\n\u001b[0;32m   1965\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_resampler\u001b[39m(obj: Series \u001b[38;5;241m|\u001b[39m DataFrame, kind\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Resampler:\n\u001b[0;32m   1966\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m   1967\u001b[0m \u001b[38;5;124;03m    Create a TimeGrouper and return our resampler.\u001b[39;00m\n\u001b[0;32m   1968\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1969\u001b[0m     tg \u001b[38;5;241m=\u001b[39m TimeGrouper(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m   1970\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m tg\u001b[38;5;241m.\u001b[39m_get_resampler(obj, kind\u001b[38;5;241m=\u001b[39mkind)\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:2046\u001b[0m, in \u001b[0;36mTimeGrouper.__init__\u001b[1;34m(self, freq, closed, label, how, axis, fill_method, limit, kind, convention, origin, offset, group_keys, **kwargs)\u001b[0m\n\u001b[0;32m   2043\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m convention \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstart\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mend\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124me\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m\"\u001b[39m}:\n\u001b[0;32m   2044\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnsupported value \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconvention\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for `convention`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 2046\u001b[0m freq \u001b[38;5;241m=\u001b[39m to_offset(freq)\n\u001b[0;32m   2048\u001b[0m end_types \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mW\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m   2049\u001b[0m rule \u001b[38;5;241m=\u001b[39m freq\u001b[38;5;241m.\u001b[39mrule_code\n",
      "File \u001b[1;32moffsets.pyx:4460\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4557\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: ME"
     ]
    }
   ],
   "source": [
    "data_new = data['1995-01-01':'2024-12-13'].copy()\n",
    "Month_data = data_new.resample('ME')['Return'].apply(lambda x:(1+x).prod()-1).to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 画图Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax. Perhaps you forgot a comma? (655327086.py, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[16], line 5\u001b[1;36m\u001b[0m\n\u001b[1;33m    color = 'b'#图片颜色\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n"
     ]
    }
   ],
   "source": [
    "fig,ax = plt.subplots(figsize = (10,5))\n",
    "\n",
    "ax.plot('Return',#图片数据\n",
    "        '*-',#图片类型\n",
    "        color = 'b'#图片颜色\n",
    "        lable = 'Monthly Return'#图片标签\n",
    "        linewidth = 1,#图片线宽\n",
    "        data='Month_data')#图片数据来源\n",
    "ax.set_title(\"China's Stock Maket Return\")# 图片标题\n",
    "ax.set_ylabel('Return')\n",
    "ax.set_xlabel('Year')\n",
    "\n",
    "#设置x轴的日期显示格式\n",
    "data_format = mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(data_format)\n",
    "ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "\n",
    "#转置x轴的日期显示格式\n",
    "plt.xticks(rotation = 90)\n",
    "\n",
    "#添加图例\n",
    "plt.legend(loc='upper right',fontsize = 8)\n",
    "fig.savefig('China_stock_Market_Return.png',dpi=300)\n",
    "\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function plot in module matplotlib.pyplot:\n",
      "\n",
      "plot(*args: 'float | ArrayLike | str', scalex: 'bool' = True, scaley: 'bool' = True, data=None, **kwargs) -> 'list[Line2D]'\n",
      "    Plot y versus x as lines and/or markers.\n",
      "    \n",
      "    Call signatures::\n",
      "    \n",
      "        plot([x], y, [fmt], *, data=None, **kwargs)\n",
      "        plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)\n",
      "    \n",
      "    The coordinates of the points or line nodes are given by *x*, *y*.\n",
      "    \n",
      "    The optional parameter *fmt* is a convenient way for defining basic\n",
      "    formatting like color, marker and linestyle. It's a shortcut string\n",
      "    notation described in the *Notes* section below.\n",
      "    \n",
      "    >>> plot(x, y)        # plot x and y using default line style and color\n",
      "    >>> plot(x, y, 'bo')  # plot x and y using blue circle markers\n",
      "    >>> plot(y)           # plot y using x as index array 0..N-1\n",
      "    >>> plot(y, 'r+')     # ditto, but with red plusses\n",
      "    \n",
      "    You can use `.Line2D` properties as keyword arguments for more\n",
      "    control on the appearance. Line properties and *fmt* can be mixed.\n",
      "    The following two calls yield identical results:\n",
      "    \n",
      "    >>> plot(x, y, 'go--', linewidth=2, markersize=12)\n",
      "    >>> plot(x, y, color='green', marker='o', linestyle='dashed',\n",
      "    ...      linewidth=2, markersize=12)\n",
      "    \n",
      "    When conflicting with *fmt*, keyword arguments take precedence.\n",
      "    \n",
      "    \n",
      "    **Plotting labelled data**\n",
      "    \n",
      "    There's a convenient way for plotting objects with labelled data (i.e.\n",
      "    data that can be accessed by index ``obj['y']``). Instead of giving\n",
      "    the data in *x* and *y*, you can provide the object in the *data*\n",
      "    parameter and just give the labels for *x* and *y*::\n",
      "    \n",
      "    >>> plot('xlabel', 'ylabel', data=obj)\n",
      "    \n",
      "    All indexable objects are supported. This could e.g. be a `dict`, a\n",
      "    `pandas.DataFrame` or a structured numpy array.\n",
      "    \n",
      "    \n",
      "    **Plotting multiple sets of data**\n",
      "    \n",
      "    There are various ways to plot multiple sets of data.\n",
      "    \n",
      "    - The most straight forward way is just to call `plot` multiple times.\n",
      "      Example:\n",
      "    \n",
      "      >>> plot(x1, y1, 'bo')\n",
      "      >>> plot(x2, y2, 'go')\n",
      "    \n",
      "    - If *x* and/or *y* are 2D arrays a separate data set will be drawn\n",
      "      for every column. If both *x* and *y* are 2D, they must have the\n",
      "      same shape. If only one of them is 2D with shape (N, m) the other\n",
      "      must have length N and will be used for every data set m.\n",
      "    \n",
      "      Example:\n",
      "    \n",
      "      >>> x = [1, 2, 3]\n",
      "      >>> y = np.array([[1, 2], [3, 4], [5, 6]])\n",
      "      >>> plot(x, y)\n",
      "    \n",
      "      is equivalent to:\n",
      "    \n",
      "      >>> for col in range(y.shape[1]):\n",
      "      ...     plot(x, y[:, col])\n",
      "    \n",
      "    - The third way is to specify multiple sets of *[x]*, *y*, *[fmt]*\n",
      "      groups::\n",
      "    \n",
      "      >>> plot(x1, y1, 'g^', x2, y2, 'g-')\n",
      "    \n",
      "      In this case, any additional keyword argument applies to all\n",
      "      datasets. Also, this syntax cannot be combined with the *data*\n",
      "      parameter.\n",
      "    \n",
      "    By default, each line is assigned a different style specified by a\n",
      "    'style cycle'. The *fmt* and line property parameters are only\n",
      "    necessary if you want explicit deviations from these defaults.\n",
      "    Alternatively, you can also change the style cycle using\n",
      "    :rc:`axes.prop_cycle`.\n",
      "    \n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    x, y : array-like or scalar\n",
      "        The horizontal / vertical coordinates of the data points.\n",
      "        *x* values are optional and default to ``range(len(y))``.\n",
      "    \n",
      "        Commonly, these parameters are 1D arrays.\n",
      "    \n",
      "        They can also be scalars, or two-dimensional (in that case, the\n",
      "        columns represent separate data sets).\n",
      "    \n",
      "        These arguments cannot be passed as keywords.\n",
      "    \n",
      "    fmt : str, optional\n",
      "        A format string, e.g. 'ro' for red circles. See the *Notes*\n",
      "        section for a full description of the format strings.\n",
      "    \n",
      "        Format strings are just an abbreviation for quickly setting\n",
      "        basic line properties. All of these and more can also be\n",
      "        controlled by keyword arguments.\n",
      "    \n",
      "        This argument cannot be passed as keyword.\n",
      "    \n",
      "    data : indexable object, optional\n",
      "        An object with labelled data. If given, provide the label names to\n",
      "        plot in *x* and *y*.\n",
      "    \n",
      "        .. note::\n",
      "            Technically there's a slight ambiguity in calls where the\n",
      "            second label is a valid *fmt*. ``plot('n', 'o', data=obj)``\n",
      "            could be ``plt(x, y)`` or ``plt(y, fmt)``. In such cases,\n",
      "            the former interpretation is chosen, but a warning is issued.\n",
      "            You may suppress the warning by adding an empty format string\n",
      "            ``plot('n', 'o', '', data=obj)``.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    list of `.Line2D`\n",
      "        A list of lines representing the plotted data.\n",
      "    \n",
      "    Other Parameters\n",
      "    ----------------\n",
      "    scalex, scaley : bool, default: True\n",
      "        These parameters determine if the view limits are adapted to the\n",
      "        data limits. The values are passed on to\n",
      "        `~.axes.Axes.autoscale_view`.\n",
      "    \n",
      "    **kwargs : `~matplotlib.lines.Line2D` properties, optional\n",
      "        *kwargs* are used to specify properties like a line label (for\n",
      "        auto legends), linewidth, antialiasing, marker face color.\n",
      "        Example::\n",
      "    \n",
      "        >>> plot([1, 2, 3], [1, 2, 3], 'go-', label='line 1', linewidth=2)\n",
      "        >>> plot([1, 2, 3], [1, 4, 9], 'rs', label='line 2')\n",
      "    \n",
      "        If you specify multiple lines with one plot call, the kwargs apply\n",
      "        to all those lines. In case the label object is iterable, each\n",
      "        element is used as labels for each set of data.\n",
      "    \n",
      "        Here is a list of available `.Line2D` properties:\n",
      "    \n",
      "        Properties:\n",
      "        agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image\n",
      "        alpha: scalar or None\n",
      "        animated: bool\n",
      "        antialiased or aa: bool\n",
      "        clip_box: `~matplotlib.transforms.BboxBase` or None\n",
      "        clip_on: bool\n",
      "        clip_path: Patch or (Path, Transform) or None\n",
      "        color or c: color\n",
      "        dash_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}\n",
      "        dash_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}\n",
      "        dashes: sequence of floats (on/off ink in points) or (None, None)\n",
      "        data: (2, N) array or two 1D arrays\n",
      "        drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}, default: 'default'\n",
      "        figure: `~matplotlib.figure.Figure`\n",
      "        fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}\n",
      "        gapcolor: color or None\n",
      "        gid: str\n",
      "        in_layout: bool\n",
      "        label: object\n",
      "        linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}\n",
      "        linewidth or lw: float\n",
      "        marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle`\n",
      "        markeredgecolor or mec: color\n",
      "        markeredgewidth or mew: float\n",
      "        markerfacecolor or mfc: color\n",
      "        markerfacecoloralt or mfcalt: color\n",
      "        markersize or ms: float\n",
      "        markevery: None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]\n",
      "        mouseover: bool\n",
      "        path_effects: list of `.AbstractPathEffect`\n",
      "        picker: float or callable[[Artist, Event], tuple[bool, dict]]\n",
      "        pickradius: float\n",
      "        rasterized: bool\n",
      "        sketch_params: (scale: float, length: float, randomness: float)\n",
      "        snap: bool or None\n",
      "        solid_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}\n",
      "        solid_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}\n",
      "        transform: unknown\n",
      "        url: str\n",
      "        visible: bool\n",
      "        xdata: 1D array\n",
      "        ydata: 1D array\n",
      "        zorder: float\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    scatter : XY scatter plot with markers of varying size and/or color (\n",
      "        sometimes also called bubble chart).\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    **Format Strings**\n",
      "    \n",
      "    A format string consists of a part for color, marker and line::\n",
      "    \n",
      "        fmt = '[marker][line][color]'\n",
      "    \n",
      "    Each of them is optional. If not provided, the value from the style\n",
      "    cycle is used. Exception: If ``line`` is given, but no ``marker``,\n",
      "    the data will be a line without markers.\n",
      "    \n",
      "    Other combinations such as ``[color][marker][line]`` are also\n",
      "    supported, but note that their parsing may be ambiguous.\n",
      "    \n",
      "    **Markers**\n",
      "    \n",
      "    =============   ===============================\n",
      "    character       description\n",
      "    =============   ===============================\n",
      "    ``'.'``         point marker\n",
      "    ``','``         pixel marker\n",
      "    ``'o'``         circle marker\n",
      "    ``'v'``         triangle_down marker\n",
      "    ``'^'``         triangle_up marker\n",
      "    ``'<'``         triangle_left marker\n",
      "    ``'>'``         triangle_right marker\n",
      "    ``'1'``         tri_down marker\n",
      "    ``'2'``         tri_up marker\n",
      "    ``'3'``         tri_left marker\n",
      "    ``'4'``         tri_right marker\n",
      "    ``'8'``         octagon marker\n",
      "    ``'s'``         square marker\n",
      "    ``'p'``         pentagon marker\n",
      "    ``'P'``         plus (filled) marker\n",
      "    ``'*'``         star marker\n",
      "    ``'h'``         hexagon1 marker\n",
      "    ``'H'``         hexagon2 marker\n",
      "    ``'+'``         plus marker\n",
      "    ``'x'``         x marker\n",
      "    ``'X'``         x (filled) marker\n",
      "    ``'D'``         diamond marker\n",
      "    ``'d'``         thin_diamond marker\n",
      "    ``'|'``         vline marker\n",
      "    ``'_'``         hline marker\n",
      "    =============   ===============================\n",
      "    \n",
      "    **Line Styles**\n",
      "    \n",
      "    =============    ===============================\n",
      "    character        description\n",
      "    =============    ===============================\n",
      "    ``'-'``          solid line style\n",
      "    ``'--'``         dashed line style\n",
      "    ``'-.'``         dash-dot line style\n",
      "    ``':'``          dotted line style\n",
      "    =============    ===============================\n",
      "    \n",
      "    Example format strings::\n",
      "    \n",
      "        'b'    # blue markers with default shape\n",
      "        'or'   # red circles\n",
      "        '-g'   # green solid line\n",
      "        '--'   # dashed line with default color\n",
      "        '^k:'  # black triangle_up markers connected by a dotted line\n",
      "    \n",
      "    **Colors**\n",
      "    \n",
      "    The supported color abbreviations are the single letter codes\n",
      "    \n",
      "    =============    ===============================\n",
      "    character        color\n",
      "    =============    ===============================\n",
      "    ``'b'``          blue\n",
      "    ``'g'``          green\n",
      "    ``'r'``          red\n",
      "    ``'c'``          cyan\n",
      "    ``'m'``          magenta\n",
      "    ``'y'``          yellow\n",
      "    ``'k'``          black\n",
      "    ``'w'``          white\n",
      "    =============    ===============================\n",
      "    \n",
      "    and the ``'CN'`` colors that index into the default property cycle.\n",
      "    \n",
      "    If the color is the only part of the format string, you can\n",
      "    additionally use any  `matplotlib.colors` spec, e.g. full names\n",
      "    (``'green'``) or hex strings (``'#008000'``).\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(plt.plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Invalid frequency: QE",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4447\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'QE'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4549\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4453\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: QE",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m Quarterly_data \u001b[38;5;241m=\u001b[39m data_new\u001b[38;5;241m.\u001b[39mresample(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mQE\u001b[39m\u001b[38;5;124m'\u001b[39m)[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x:(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39mx)\u001b[38;5;241m.\u001b[39mprod()\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m.\u001b[39mto_frame()\n\u001b[0;32m      2\u001b[0m Yearly_data \u001b[38;5;241m=\u001b[39m data_new\u001b[38;5;241m.\u001b[39mresample(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYE\u001b[39m\u001b[38;5;124m'\u001b[39m)[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x:(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39mx)\u001b[38;5;241m.\u001b[39mprod()\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m.\u001b[39mto_frame()\n\u001b[0;32m      3\u001b[0m Quarterly_data\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:9439\u001b[0m, in \u001b[0;36mNDFrame.resample\u001b[1;34m(self, rule, axis, closed, label, convention, kind, on, level, origin, offset, group_keys)\u001b[0m\n\u001b[0;32m   9436\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   9437\u001b[0m     axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m-> 9439\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m get_resampler(\n\u001b[0;32m   9440\u001b[0m     cast(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSeries | DataFrame\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m),\n\u001b[0;32m   9441\u001b[0m     freq\u001b[38;5;241m=\u001b[39mrule,\n\u001b[0;32m   9442\u001b[0m     label\u001b[38;5;241m=\u001b[39mlabel,\n\u001b[0;32m   9443\u001b[0m     closed\u001b[38;5;241m=\u001b[39mclosed,\n\u001b[0;32m   9444\u001b[0m     axis\u001b[38;5;241m=\u001b[39maxis,\n\u001b[0;32m   9445\u001b[0m     kind\u001b[38;5;241m=\u001b[39mkind,\n\u001b[0;32m   9446\u001b[0m     convention\u001b[38;5;241m=\u001b[39mconvention,\n\u001b[0;32m   9447\u001b[0m     key\u001b[38;5;241m=\u001b[39mon,\n\u001b[0;32m   9448\u001b[0m     level\u001b[38;5;241m=\u001b[39mlevel,\n\u001b[0;32m   9449\u001b[0m     origin\u001b[38;5;241m=\u001b[39morigin,\n\u001b[0;32m   9450\u001b[0m     offset\u001b[38;5;241m=\u001b[39moffset,\n\u001b[0;32m   9451\u001b[0m     group_keys\u001b[38;5;241m=\u001b[39mgroup_keys,\n\u001b[0;32m   9452\u001b[0m )\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:1969\u001b[0m, in \u001b[0;36mget_resampler\u001b[1;34m(obj, kind, **kwds)\u001b[0m\n\u001b[0;32m   1965\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_resampler\u001b[39m(obj: Series \u001b[38;5;241m|\u001b[39m DataFrame, kind\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Resampler:\n\u001b[0;32m   1966\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m   1967\u001b[0m \u001b[38;5;124;03m    Create a TimeGrouper and return our resampler.\u001b[39;00m\n\u001b[0;32m   1968\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1969\u001b[0m     tg \u001b[38;5;241m=\u001b[39m TimeGrouper(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m   1970\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m tg\u001b[38;5;241m.\u001b[39m_get_resampler(obj, kind\u001b[38;5;241m=\u001b[39mkind)\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:2046\u001b[0m, in \u001b[0;36mTimeGrouper.__init__\u001b[1;34m(self, freq, closed, label, how, axis, fill_method, limit, kind, convention, origin, offset, group_keys, **kwargs)\u001b[0m\n\u001b[0;32m   2043\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m convention \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstart\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mend\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124me\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m\"\u001b[39m}:\n\u001b[0;32m   2044\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnsupported value \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconvention\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for `convention`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 2046\u001b[0m freq \u001b[38;5;241m=\u001b[39m to_offset(freq)\n\u001b[0;32m   2048\u001b[0m end_types \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mW\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m   2049\u001b[0m rule \u001b[38;5;241m=\u001b[39m freq\u001b[38;5;241m.\u001b[39mrule_code\n",
      "File \u001b[1;32moffsets.pyx:4460\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4557\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: QE"
     ]
    }
   ],
   "source": [
    "Quarterly_data = data_new.resample('QE')['Return'].apply(lambda x:(1+x).prod()-1).to_frame()\n",
    "Yearly_data = data_new.resample('YE')['Return'].apply(lambda x:(1+x).prod()-1).to_frame()\n",
    "Quarterly_data\n",
    "Yearly_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[19], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2016\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2016\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mmean()\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "Month_data['2016':'2016']['Return'].mean()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
